<template>
  <div>
    <el-form
      :model="updateForm"
      :rules="rules"
      ref="updateForm"
      label-width="100px"
      class="demo-ruleForm"
      @submit.native.prevent
    >
      <el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="请求方法" prop="requestMethod">
              <el-select style="width: 100%" v-model="updateForm.requestMethod" placeholder="请选择请求方法">
                <el-option label="GET" value="GET"></el-option>
                <el-option label="POST" value="POST"></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="请求地址" prop="requestUrl">
              <el-input v-model="updateForm.requestUrl" placeholder="请输入数据名称" clearable></el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="映射类型" prop="mappingType">
              <!--<select-tree placeholder="请选择模板" :selectedValue.sync="updateForm.fkDatasourceTypeCode"-->
              <!--:treeList="treeList"-->
              <!--:props="treeProps"></select-tree>-->
              <el-select style="width: 100%" v-model="updateForm.mappingType" placeholder="请选择映射类型">
                <el-option value="forward" label="forward"></el-option>
                <el-option value="redirect" label="redirect"></el-option>
                <el-option value="view" label="view"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="目标" prop="地址">
              <el-input v-model="updateForm.targetPage" placeholder="映射类型为view时无效" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="模板" prop="requestTpl">
              <!--<select-tree placeholder="请选择模板" :selectedValue.sync="updateForm.fkDatasourceTypeCode"-->
              <!--:treeList="treeList"-->
              <!--:props="treeProps"></select-tree>-->
              <el-select style="width: 100%" v-model="updateForm.requestTpl" placeholder="请选择模板">
                <el-option v-for="(row,key) in templateList" v :label="row.fileName" :value="key"></el-option>

                <!--                <el-option v-for="(row,key) in templateList" v :label="row.fileName" :value="row.fileName"></el-option>-->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="静态化" prop="pageReWrite">
              <el-input v-model="updateForm.pageReWrite" placeholder="请输入静态化时长-1代表永久(秒)" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <code-editor ref="code" currentTheme="solarized-dark" :codeText.sync="updateForm.encoderScript"
                       :toolbarShow="true" height="55vh"></code-editor>
        </el-row>
      </el-row>

    </el-form>
  </div>
</template>
<script>
  import HttpCode from '@/js/httpCode.js'
  import cmsResourceApi from '@/apis/cmsApp/cmsResourceApi.js'
  import cmsTbRequestMappingApi from '@/apis/cmsApp/cmsTbRequestMappingApi.js'

  import CodeEditor from '../../../components/codeEditor/CodeEditor'

  export default {
    name: 'CmsTbRequestMappingUpdate',
    components: {
      CodeEditor,
      SelectTree: () => import('@/components/selecttree/SelectTree.vue')
    },
    data () {
      return {
        updateForm: {
          id: '',
          asyncMode: '3',
          disabled: 0,
          flowCode: '',
          flowName: '',
          flowVersion: '',
          isDefault: 1,
          sceneCode: '01',
        },
        templateList: [],
        rules: {
          // flowName: [
          //   {required: true, message: '请输入产品名称', trigger: 'blur'},
          //   {min: 2, max: 32, message: '产品名称长度2-32字符', trigger: 'blur'}
          // ],
          // flowCode: [
          //   {required: true, message: '请输入用户昵称', trigger: 'change'}
          // ],
          // sceneCode: [
          //   {required: true, message: '请输入用户昵称', trigger: 'change'}
          // ]
        }
      }
    },
    created () {
      //可能需要特殊处理，手动赋值
      if (this.data != null) {
        this.updateForm = this.data
      }
      this.doSelectTemplates()
    },
    mounted () {
      this.$refs['code'].refresh()
    },
    methods: {
      submitForm () {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            this.submitFormHandle()
          } else {
            this.cancelSubmitForm()
          }
        })
      },
      doSelectTemplates () {
        cmsResourceApi.selectTemplates().then(msg => {
          this.templateList = msg.rows
        })
      },
      submitFormHandle () {
        if (this.updateForm.id == undefined || this.updateForm.id == '') {
          cmsTbRequestMappingApi
            .addService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            })
            .catch(msg => {
              if ((msg.cdoe = 400)) {
                this.formError(msg)
              }
              this.cancelSubmitForm()
            })
        } else {
          cmsTbRequestMappingApi.updateService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            }).catch(msg => {
            if ((msg.cdoe = 400)) {
              this.formError(msg)
            }
            this.cancelSubmitForm()
          })
        }
      },
      submitFormSuccess (msg) {
        this.$store.commit('successSubmitForm', {
          name: this.formKey,
          data: msg
        })
      },
      cancelSubmitForm () {
        this.$store.commit('cancelSubmitForm', {name: this.formKey})
      },
      formError (msg) {
        if (msg.code == HttpCode.E400) {
          for (let row of msg.errors) {
            let fields = this.$refs['updateForm'].fields.filter(it => {
              return it.prop == row.errName
            })
            if (fields.length > 0) {
              fields[0].validateMessage = row.errMsg
              fields[0].validateState = 'error'
            }
          }
        }
      }
    },
    props: {
      treeList: {
        type: Array,
        default: []
      },
      formKey: {
        type: String,
        default: 'currentForm'
      },
      data: {
        type: Object,
        default: {}
      }
    },
    computed: {
      submitFormEventListener () {
        return this.$store.getters.onEvent(`${this.formKey}-submitForm`)
      }
    },
    watch: {
      submitFormEventListener: function (a, b) {
        if (a == null || a == b) {
          return null
        }
        this.submitForm()
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
</style>
